package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.OrderService;
import com.itheima.health.util.IDCardUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

/**
 *
 * @author zhangmeng
 * @description 检查组service实现类
 * @date 2019/9/16
 **/
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;

    @Transactional
    @Override
    public void add(Map<String, String> map) {
        log.info("[检查项-添加]data：{}", map);
        String idCard = map.get("idCard");
        Member member = new Member();
        try {
            boolean b = IDCardUtils.IDCardValidate(idCard);
            if (b == false) throw new BusinessRuntimeException("身份证号输入错误,请重新输入");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        try {
            Date parse = simpleDateFormat.parse(format);
            member.setRegTime(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(date);
        member.setIdCard(idCard);
        member.setName(map.get("name"));
        member.setPhoneNumber(map.get("telephone"));
        member.setSex(map.get("sex"));
        Member member1 = memberDao.selectByIDCard(idCard);
        if (member1!= null) {
            if (orderDao.selectByMemberId(memberDao.selectByIDCard(idCard).getId()).getOrderStatus().equals("未到诊")) {
                throw new BusinessRuntimeException("您预约其他套餐，不可重复预约");
            }
        }
            memberDao.insert(member);
            Order order = new Order();
            order.setMemberId(member.getId());
            Date orderDate = null;
            try {
                orderDate = simpleDateFormat.parse(map.get("orderDate"));
                order.setOrderDate(orderDate);
                System.out.println(orderDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            order.setOrderType("微信预约");
            order.setOrderStatus("未到诊");
            String setmealId = map.get("setmealId");
            int i = Integer.parseInt(setmealId);
            order.setSetmealId(i);
            orderDao.insert(order);
            OrderSetting orderDate1 = orderDao.findOrderDate(orderDate);
            if (orderDate1.getNumber() == orderDate1.getReservations()) {
                throw new BusinessRuntimeException("预约已满请选择其他日期");
            } else {
                orderDate1.setReservations(orderDate1.getReservations() + 1);
                orderDao.updateReservationsByOrderDate(orderDate1);
            }
        }

        @Override
        public Map<String, Object> findById (Integer id){
            return orderDao.selectById(id);
        }



}